package com.example.coursemanagement.controller;

import com.example.coursemanagement.MyUtils.FileNameUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Arrays;

@RequestMapping("file")
@RestController
public class UploadFileController {

//    //文件扩展名校验
//    private String arrsuffix[] = {"PNG","JPG","JPEG","BMP","GIF","SVG"};
//    @Value("${file.path}")
//    private String uploadPath;
//
//    @RequestMapping(value = "/upload")
//    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
//        try {
//            if (file.isEmpty()) {
//                return "上传图片服务器内容为空";
//            } else {
//                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
//                if (!Arrays.asList(arrsuffix).contains(suffix.toUpperCase())) {
//                    return "请选择正确格式的文件";
//                }
//                //获取文件名称
//                String filenameup = file.getOriginalFilename();
//
//                //定义要上传文件 的存放路径
//                String localPath = uploadPath + "/";
//                filenameup = FileNameUtil.getFileName(filenameup);
//                File dest = new File(localPath,filenameup);
//                if (!dest.exists()) {
//                    dest.mkdirs();
//                }
//                file.transferTo(dest);//拷贝文件到指定路径储存
//                return filenameup;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        // 返回
//        return "未知错误导致上传失败";
//    }
}
